package cn.cdut.servlet;

import cn.cdut.servlet.tools.JDBC.Sign;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/Welcome.jsp")
public class Filter implements javax.servlet.Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        String [] urls={"Login.jsp","register.jsp",""};

        HttpServletRequest request=(HttpServletRequest) servletRequest;
//        //拿到用户请求路径
//        String url= request.getRequestURI().toString();
//        for (String u : urls) {
//            if(u.equals(url)){
//                filterChain.doFilter(servletRequest,servletResponse);
//                return ;
//            }
//        }

        HttpSession session= request.getSession();
        Object user=session.getAttribute("email");
        if(user!=null)
        {
            filterChain.doFilter(request,servletResponse);
        }
        else{
            Sign.dflag=3;
            request.getRequestDispatcher("/login.jsp").forward(request,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
